package com.lfy.fyadmin.controller;

import com.lfy.fyadmin.common.R;
import com.lfy.fyadmin.dto.CaptchaResponse;
import com.lfy.fyadmin.dto.LoginRequest;
import com.lfy.fyadmin.dto.LoginResponse;
import com.lfy.fyadmin.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/api/auth")
@Tag(name = "认证管理", description = "用户认证相关接口")
public class AuthController {
    
    private final AuthService authService;
    
    public AuthController(AuthService authService) {
        this.authService = authService;
    }
    
    @GetMapping("/captcha")
    @Operation(summary = "获取图形验证码", description = "生成图形验证码，返回验证码key和Base64编码的图片")
    public R<CaptchaResponse> getCaptcha() {
        CaptchaResponse captcha = authService.generateCaptcha();
        return R.ok(captcha);
    }
    
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口，需要提供用户名、密码和验证码")
    public R<LoginResponse> login(@Valid @RequestBody LoginRequest loginRequest) {
        LoginResponse loginResponse = authService.login(loginRequest);
        return R.ok(loginResponse);
    }
    
    @PostMapping("/logout")
    @Operation(summary = "用户登出", description = "用户登出接口")
    public R<Void> logout() {
        authService.logout();
        return R.ok();
    }
}